package com.fancy.learncenter.ui.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.CartoonRealeaseSpeakDataBean;
import com.fancy.learncenter.ui.adapter.CartoonListenLocalAdapter;
import com.fancy.learncenter.ui.view.PromptPopupWindow;
import com.fancy.learncenter.utils.FileUtils;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.Utils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A fragment with a Google +1 button.
 */
public class CartoonLocalListenFragment extends Fragment {


    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.ll_root)
    LinearLayout llRoot;

//    public CartoonLocalListenFragment(String fileResPath) {
//        this.fileResPath  =fileResPath;
//        // Required empty public constructor
//    }


    public CartoonLocalListenFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            this.fileResPath = getArguments().getString("fileResPath");
            LogUtil.MyLog("=====initDatas===", "===onCreate======" + fileResPath);

        }
    }

    public static CartoonLocalListenFragment newInstance(String fileResPath) {
        LogUtil.MyLog("=====initDatas===", "====CartoonLocalListenFragment=====" + fileResPath);
        Bundle args = new Bundle();
        args.putString("fileResPath", fileResPath);

        CartoonLocalListenFragment fragment = new CartoonLocalListenFragment();
        fragment.setArguments(args);
        return fragment;
    }


    View rootView;
    List<CartoonRealeaseSpeakDataBean.ResultBean> localDatasPath = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_cartoon_release_listen, container, false);
        ButterKnife.bind(this, rootView);

        getLocalRes();

        initDatas();

        initView();
        return rootView;
    }

    String fileResPath;

    @Override
    public void onResume() {
        super.onResume();

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    PromptPopupWindow promptPopupWindow;

    private void initView() {
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        adapter = new CartoonListenLocalAdapter(getActivity(), localDatasPath);
        adapter.setDeleteCallBack(new CartoonListenLocalAdapter.DeleteCallBack() {
            @Override
            public void deleCallBack(final int position) {
                if (promptPopupWindow == null) {
                    promptPopupWindow = new PromptPopupWindow(getActivity(), llRoot);
                    promptPopupWindow.setPromptTitle("是否删除本地录音？");
                    promptPopupWindow.setCallBack(new PromptPopupWindow.CallBack() {
                        @Override
                        public void confirm() {
                            FileUtils.deleteFile(localDatasPath.get(position).getLocalResourceUrl());
                            localDatasPath.remove(position);
                            adapter.notifyDataSetChanged();

                        }

                        @Override
                        public void cancle() {

                        }
                    });
                }
                promptPopupWindow.showPopupWindow();

            }
        });
        adapter.setFilePath(fileResPath);
        recyclerView.setAdapter(adapter);
    }

    CartoonListenLocalAdapter adapter;
//    List<CartoonRealeaseSpeakDataBean.ResultBean> localPathData = new ArrayList<>();

    private void initDatas() {
        LogUtil.MyLog("=====initDatas===", "=========" + fileResPath);
        File file = new File(fileResPath);
        File[] tempList = file.listFiles();

        if (tempList == null) {
            return;
        }
        for (int i = 0; i < tempList.length; i++) {
            String res = resDatas.get(tempList[i].getName());

            if (!TextUtils.isEmpty(res)) {

                String[] data = res.split("&&");

                CartoonRealeaseSpeakDataBean.ResultBean realeaseSpeakDataBean = new CartoonRealeaseSpeakDataBean.ResultBean();

                if (data == null || data.length != 5) {
                    return;
                }
                realeaseSpeakDataBean.setCoverImgUrl(data[0]);

                realeaseSpeakDataBean.setQuestionName(data[1]);
//                    if (Utils.isNumeric(data[2])) {
//                        realeaseSpeakDataBean.setResourceDuration(Long.parseLong(data[2]));
//                    }

                realeaseSpeakDataBean.setCommitTime(data[3]);

                realeaseSpeakDataBean.setSubjectQuestionId(data[4]);

                realeaseSpeakDataBean.setLocalResourceUrl(tempList[i].getAbsolutePath());

                localDatasPath.add(realeaseSpeakDataBean);
            }
        }
    }

    Map<String, String> resDatas = new HashMap();

    private void getLocalRes() {
        String readResultStr = FileUtils.readFromSD(FileUtils.getSutendLocalRes());
        if (!TextUtils.isEmpty(readResultStr)) {
            resDatas = Utils.transStringToMap(readResultStr);
        }
    }
}
